package com.hospital.endescryption.async;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hospital.common.core.constants.InsuranceDeptEnum;
import com.hospital.common.core.exception.ExceptionHelpEnum;
import com.hospital.common.core.module.entity.BaseRequest;
import com.hospital.common.core.module.entity.NFC.ORG_RES;
import com.hospital.common.core.result.ErrorTip;
import com.hospital.common.core.result.SuccessTip;
import com.hospital.common.core.result.Tip;
import com.hospital.common.core.utils.BeanValidator;
import com.hospital.common.core.utils.Sm2Demo;
import com.hospital.common.core.utils.Sm3Demo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * timetask
 *
 * @description 处理微信支付功能
 * @author:louislau
 * @create 2019-11-08 08:31:47
 **/
@Service
@Slf4j
public class WxPayFunc {

    @Async
    public void wxPay(BaseRequest params) {
        try {
            String result = HttpUtil.post(params.getUrl(), JSON.toJSONString(params));
            JSONObject convertValue = JSON.parseObject(result);
            if ("0000".equals(convertValue.getString("code"))) {
                log.info(params + ":执行成功");
            } else {
                log.info(params + ":执行失败--" + result);
            }
        } catch (Exception e) {
            log.error(e.toString() + "====================请求参数：" + JSON.toJSONString(params));
        }
    }

    /**
     * 同步推送消息至移动医保平台
     *
     * @param params
     * @return
     */
    public Tip pushMsg(String params, String url,String appId,String appSecret) {
        Tip tip = new SuccessTip();
        try {
            //直接调用医保时的入参设置及返回解析 start
//            //设置请求头
////            HttpHeaders httpHeaders = new HttpHeaders();
////            //传递请求体时必须设置传递参数的格式，为Content-Type ： application/json
////            httpHeaders.add("Content-Type", "application/json;charset=UTF-8");
////            // 2.请求头 & 请求体
////            HttpEntity<JSON> fromEntity = new HttpEntity(params, httpHeaders);
////            JSONObject  response = restTemplate.postForObject(url, fromEntity, JSONObject.class);
            //直接调用医保时的入参设置及返回解析  stop

            //调用平台的方式  start
            String result = HttpUtil.post(url, JSON.toJSONString(params));
//            if (responseEntity.getStatusCodeValue() == 200) {
            JSONObject response = JSON.parseObject(result);
            //调用平台的方式  stop
            JSONObject convertValue = Sm3Demo.denc(response,appId,appSecret);
            if ("000000".equals(convertValue.getString("respCode"))) {
                tip.setData(convertValue);
            } else {
                tip = new ErrorTip(302, convertValue.toJSONString());
            }
//            }else {
//                tip = new ErrorTip(304, JSON.toJSONString(result));
//            }
        } catch (Exception e) {
            tip = new ErrorTip(303, e.getMessage());
        }
        return tip;
    }

    /**
     * 参数校验
     *
     * @param object
     * @return
     */
    public ORG_RES validateFunc(Object object) {
        try {
            Map<String, String> validateobject = BeanValidator.validateobject(object);
            if (validateobject != null && validateobject.entrySet().size() > 0) {
                String errStr = "";
                for (Map.Entry<String, String> entry : validateobject.entrySet()) {
                    errStr = errStr + entry.getKey() + "->" + entry.getValue() + ";";
                }
                InsuranceDeptEnum.HOS_ERROR_PARAM.setMessage(errStr);
                return new ORG_RES(InsuranceDeptEnum.HOS_ERROR_PARAM);
            }

        } catch (Exception e) {
            return new ORG_RES(InsuranceDeptEnum.HOS_ERROR_PARAM);
        }
        return new ORG_RES();
    }

    /**
     * 得到解密验签后的解密数据类容
     *
     * @param object
     * @return
     */
    public Tip getLastClass(JSONObject object,String appId,String appSecret,String platformPubKey) {
        String denc = null;
        //医保调用HIS接口验签
        denc = Sm2Demo.denc(object,appId,appSecret,platformPubKey);
        if (denc == null) {
            return new ErrorTip(Integer.valueOf(InsuranceDeptEnum.HOS_ERROR.getCode()),ExceptionHelpEnum.SIGN_ERR.getMessage());
        }
        return new SuccessTip(denc);
    }
}